<?php

/*
 * memcached缓存类
 */

class Cache_memcached extends Base {
	
	//默认过期时间，单位秒
	protected $expiration = 3600;
	
	//配置项
	protected $config = array(
		array(
			'host' => '127.0.0.1',
			'port' => 11211,
			'weight' => 1
		)
	);
	
	//memcached对象，可能是Memcache对象或者Memcached对象
	protected $_memcached = NULL;
	
	protected $_valid = array(
		'expiration',
		'config'
	);
	
	public function __construct($config = array())
	{
		if ($config)
		{
			$this->config($config);
		}
	}
	
	/*
	 * 初始化
	 * @access	public
	 * @return	boolean
	 */
	public function init()
	{
		if ( ! $this->config)
		{
			$this->set_error('服务器配置信息未设置！');
			return FALSE;
		}

		if (class_exists('Memcached'))
		{
			$this->_memcached = new Memcached();
		}
		elseif (class_exists('Memcache'))
		{
			$this->_memcached = new Memcache();
		}
		else
		{
			$this->set_error('Memcached或Memcache未找到！');
			return FALSE;
		}
		
		if (get_class($this->_memcached) == 'Memcached')
		{
			//Memcached
			foreach ($this->config as $config)
			{
				$this->_memcached->addServer($config['host'], $config['port'], $config['weight']);
			}
		}
		else
		{
			//Memcache
			foreach ($this->config as $config)
			{
				$this->_memcached->addServer($config['host'], $config['port'], 0, $config['weight']);
			}
		}
		
		return TRUE;
	}
	
	/*
	 * 保存
	 * @access	public
	 * @param	$id string 键名
	 * @param	$data mixed 键值数据，资源不行滴
	 * @param	$expiration integer 缓存时间
	 * @return	boolean
	 */
	public function set($id, $data, $expiration = NULL)
	{
		$expiration = ($expiration !== NULL) ? intval($expiration) : intval($this->expiration);
		$expiration = $expiration > 0 ? $expiration : 0;
		
		if (get_class($this->_memcached) == 'Memcached')
		{
			//Memcached
			return $this->_memcached->set($id, array($data, time(), $expiration), $expiration);
		}
		else
		{
			//Memcache
			return $this->_memcached->set($id, array($data, time(), $expiration), 0, $expiration);
		}
	}
	
	/*
	 * 返回缓存数据
	 * @access	public
	 * @param	$id string 键名
	 * @return	mixed
	 */
	public function get($id)
	{
		$result = $this->_memcached->get($id);
		return (is_array($result) && (count($result) == 3)) ? $result[0] : FALSE;
	}
	
	/*
	 * 返回缓存详细信息
	 * @access	public
	 * @param	$id string 键名
	 * @return	array or boolean
	 */
	public function detail($id)
	{
		$result = $this->_memcached->get($id);
		
		if ( ! is_array($result) && count($result) != 3)
		{
			return FALSE;
		}
		
		list($data, $time, $expiration) = $result;
		
		//data => 缓存数据 time => 保存时候的时间，秒 expiration => 缓存时间，秒
		return array(
			'data' => $data,
			'time' => $time,
			'expiration' => $expiration
		);
	}
	
	/*
	 * 删除
	 * @access	public
	 * @param	$id string 键名
	 * @return	boolean
	 */
	public function delete($id)
	{
		return $this->_memcached->delete($id);
	}
	
	/*
	 * 清空
	 * @access	public
	 * @return	boolean
	 */
	public function clean()
	{
		return $this->_memcached->flush();
	}
	
	public function set_config($config)
	{
		$this->config = array();
		
		if ($config && is_array($config))
		{
			foreach ($config as $c)
			{
				if (isset($c['host']) && isset($c['port']) && isset($c['weight']))
				{
					$this->config[] = $c;
				}
			}
		}
		
		return $this;
	}
	
	public function set_expiration($expiration)
	{
		$this->expiration = intval($expiration) > 0 ? intval($expiration) : 0;
		return $this;
	}
	
}